package servlet.user;

import domain.User;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/EditUserServlet")
public class EditUserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userid = req.getParameter("userid");
        String username = req.getParameter("username");
        int sex = Integer.parseInt(req.getParameter("sex"));
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        User user = new User(userid,username,sex,phone,address);
        UserService userService = new UserService();
        if (userService.EditUser(user)){
            resp.sendRedirect("User_management.jsp");
        }else {
            req.setAttribute("user", user);
            req.setAttribute("updateMsg","更新失败！！！");
            req.getRequestDispatcher("userEdit.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
